package gov.nhso.prov.acc.apps.acc7;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

import gov.nhso.prov.acc.beans.Acc7IncomeBean;
import gov.nhso.prov.acc.beans.Acc7TransactionBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

public class ACC70304 extends Utility{
	public String url = "/apps/acc7/ACC70304A.jsp";

	public ACC70304(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if (job.equals("init")) {
			this.init(request);
		} else if (job.equals("search")){
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		} else if (job.equals("view")) {
			this.view(request);
		} else if (job.equals("back")){
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		} else if (job.equals("cancel")) {
			this.cancel(request);
		}
	}
	
	private String init(HttpServletRequest request){
		this.initPage(request);
		request.setAttribute("job", "search");
		return this.url;
	}
	
	private String search(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);

			PageBean pageBean = new PageBean();
			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			String docNo = this.getAttr(request, "docNo");
			String provinceId = this.getAttr(request, "provinceId");
			String startDate = this.getAttr(request, "startDate");
			String endDate = this.getAttr(request, "endDate");
			String incomeType = this.getAttr(request, "incomeType");
			String condition = "";
			
			if(!dao.chkData(startDate).equals("")){
				startDate = this.dateUtil().changeFormatDate(startDate, "/", "/");
				condition += " AND TO_DATE(INCOME_DATE,'DD/MM/YYYY') >= TO_DATE('"+startDate+"','DD/MM/YYYY') ";
			}			
			if(!dao.chkData(endDate).equals("")){
				endDate = this.dateUtil().changeFormatDate(endDate, "/", "/");
				condition += " AND TO_DATE(INCOME_DATE,'DD/MM/YYYY') <= TO_DATE('"+endDate+"','DD/MM/YYYY') ";
			}
			incomeBean.setProvinceId(provinceId);
			incomeBean.setDocNo(docNo);
			incomeBean.setActive("Y");
			incomeBean.setIncomeStatus("Y");
			/*if (this.user(request).getAreaLevel().equals("2")){	
				condition = " AND PROVINCE_ID IN (SELECT PROVINCE_ID FROM PROVINCE WHERE NHSO_ZONE = '" + this.user(request).getAreaCode()+ "' ) ";
			}*/
			if (incomeType != null && !incomeType.equals("")) {
				condition += " AND DOC_NO LIKE '%"+incomeType+"%' ";
			}
			this.beanUtil().copyRequest2Bean(request, pageBean);
			pageBean = dao.selectPage(incomeBean,condition, pageBean);
			
			if (pageBean.getPageData() == null) {
				this.message(request).setMsgCode("I001");
			}
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("job", "search");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String view(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			
			Acc7IncomeBean incomeBean = new Acc7IncomeBean();
			this.beanUtil().copyRequest2Bean(request, incomeBean);
			Map<String, String> xcols = new HashMap<String, String>();
			xcols.put("TRANSACTION_TIMESTAMP", "TO_CHAR(TRANSACTION_TIMESTAMP, 'YYYYMMDDHH24MISSFF3')");
			dao.select(incomeBean, xcols, "");
			
			request.setAttribute("incomeBean", incomeBean);
			request.setAttribute("provinceName", this.dataUtil(dao).getProvinceName(incomeBean.getProvinceId()));
			request.setAttribute("job", "cancel");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				this.url = "/apps/acc7/ACC70304B.jsp";
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String cancel(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc7IncomeBean aib = new Acc7IncomeBean();
			Acc7TransactionBean atb = new Acc7TransactionBean();
			String transactionTimestamp = this.getAttr(request, "transactionTimestamp");
			String docNo = this.getAttr(request, "docNo");
			String userId = this.user(request).getUserId();
			
			aib.setIncomeStatus("P");
			aib.setTransactionTimestamp("null");
			aib.setDocNo(docNo);
			aib.setUpdatedBy(userId);
			aib.setUpdatedDate(this.sysDate);
			dao.update(aib, " AND TRANSACTION_TIMESTAMP = TO_TIMESTAMP('" + transactionTimestamp + "', 'YYYYMMDDHH24MISSFF3')");
			atb.setTransactionTimestamp("TO_TIMESTAMP('" + transactionTimestamp + "', 'YYYYMMDDHH24MISSFF3')");
			dao.delete(atb);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				request.setAttribute("docNo", "");
				request.setAttribute("provinceId", "");
				request.setAttribute("startDate", "");
				request.setAttribute("endDate", "");
				request.setAttribute("incomeType", "");
				request.setAttribute("transactionTimestamp", "");
				this.search(request);
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
